﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Lister;
using COFE.Core.Logger;
using System.Text.RegularExpressions;

namespace COFE.Core.Profile
{
    public static class IVolumeManagerExtension
    {
       

        #region FindVolumes

        /// <summary>
        /// Find a particular directory volume info using it's key.
        /// </summary>        
        /// <param name="listerKey">Lister key.</param>
        /// <returns></returns>
        public static IVolume FindRegisteredVolume(this IVolumeManager manager, string listerKey)
        {
            if (listerKey == ProfileRootDirectoryLister.ProfileRootListerKey)
                return COFEShared.VolumeRegistrar.Volumes[listerKey];
            return manager[listerKey, true, true];
            //Func<Dictionary<IVolume, VolumeAccessMode>, IVolume> findListerInfo =
            //    (listers) =>
            //    {
            //        return listers
            //            .Where((kvp) => { return kvp.Key.ListerKey.Equals(listerKey, StringComparison.InvariantCultureIgnoreCase); })
            //            .Select((kvp) => { return kvp.Key; })
            //            .FirstOrDefault();
            //    };

            //IVolume dListerInfo = findListerInfo(manager.PrimaryVolumes); ;

            //if (dListerInfo != null)
            //    return dListerInfo;

            //dListerInfo = findListerInfo(manager.SecondaryVolumes); ;

            //if (dListerInfo != null)
            //    return dListerInfo;

            //return null;
        }

        /// <summary>
        /// Find a particular  directory volume info using a type.
        /// </summary>
        /// <typeparam name="A">Type of directory volume.</typeparam>
        /// <returns></returns>
        public static IVolume[] FindRegisteredVolume<T>(this IVolumeManager manager)
        {
            List<IVolume> retList = new List<IVolume>();

            foreach (var info in manager.PrimaryVolumes)
                if (info.DirectoryLister.GetType().Equals(typeof(T)))
                    retList.Add(info);
            foreach (var info in manager.SecondaryVolumes)
                if (info.DirectoryLister.GetType().Equals(typeof(T)))
                    retList.Add(info);

            return retList.ToArray();
        }

        #endregion


        public static void NotifyFileSystemModified(this IVolumeManager manager, object sender, FileSystemEventArgsExA args)
        {

        }
    }
}
